package com.milhouz.wormdroid.gl.factory;

import com.milhouz.wormdroid.R;
import com.milhouz.wormdroid.gl.model.GLSprite;
import com.milhouz.wormdroid.util.StaticValues;

public class LandFactory {
	public final static int LAND_NB_ELEMENTS = 3;
	private final static String SPRITE_TYPE_LAND = "LAND";
	
	private final static int BACKGROUND_WIDTH = 800;
	private final static int BACKGROUND_HEIGHT = 48;
	private final static int BACKGROUND_WIDTH2 = 48;
	private final static int BACKGROUND_HEIGHT2 = 800;
	
	private final static int DEFAULT_ELEMENT_WIDTH_MAX = 300;
	private final static int DEFAULT_ELEMENT_HEIGHT_MAX = 30;
	private final static int DEFAULT_LAND_WIDTH= 600;
	private final static int DEFAULT_LAND_HEIGHT = 400;
	
	private final static boolean DEFAULT_LAND_FIXED = true;
	private final static float DEFAULT_LAND_ELASTIC = 0f;
	private final static float DEFAULT_LAND_FRICTION = 0f;
	private final static float DEFAULT_LAND_MASS = 1f;
	
	
	private static LandFactory instance;
	public int tmpNbTeam = 0;

	public static LandFactory getInstance() {
		if (instance == null) {
			instance = new LandFactory();
		}
		return instance;
	}

	public GLSprite[] generateLandSprites() {
		GLSprite[] lands = new GLSprite[LAND_NB_ELEMENTS];
		lands[0] = generateLand(160, 60, BACKGROUND_WIDTH, BACKGROUND_HEIGHT);
		lands[1] = generateLand(-390, 485,BACKGROUND_WIDTH2,BACKGROUND_HEIGHT2);
		lands[2] = generateLand(390, 485,BACKGROUND_WIDTH2,BACKGROUND_HEIGHT2);
		return lands;
	}

	public GLSprite generateLand(int x, int y, int witdh, int height) {
		GLSprite land = new GLSprite(StaticValues.spriteId++, x, y, witdh, height, DEFAULT_LAND_FIXED, DEFAULT_LAND_MASS, DEFAULT_LAND_ELASTIC, DEFAULT_LAND_FRICTION, R.drawable.tex_brick1);
		land.type = SPRITE_TYPE_LAND;
		return land;
	}
	
	public GLSprite[] generateBoxLandSprites() {
		GLSprite[] lands = new GLSprite[4];
		lands[0] = generateLand(0, -100, 800, DEFAULT_ELEMENT_HEIGHT_MAX);
		lands[1] = generateLand(400, 200, DEFAULT_ELEMENT_HEIGHT_MAX, 600);
		lands[2] = generateLand(-400, 200, DEFAULT_ELEMENT_HEIGHT_MAX, 600);
		lands[3] = generateLand(0, -40, 200, 20);
		return lands;
	}
	
	public GLSprite[] generateRandomLandSprites(final int nbLandElements) {
		GLSprite[] lands = new GLSprite[nbLandElements];
		final int landBucketSize = nbLandElements / 4;
    int xElement, yElement;
    int widthElement = 0;
    for (int i=0; i<nbLandElements; i++) {
        // Pick a random location for this sprite.
        xElement = (int)(Math.random() * DEFAULT_LAND_WIDTH);
        yElement = (int)(Math.random() * DEFAULT_LAND_HEIGHT);
        while (widthElement < 20) { // longueur minimale
        	widthElement = (int)(Math.random() * DEFAULT_ELEMENT_WIDTH_MAX);
        }
        if (i < landBucketSize) {
        	lands[i] = generateLand(xElement, yElement, 200, DEFAULT_ELEMENT_HEIGHT_MAX);
        } else if (i < landBucketSize * 2) {
        	lands[i] = generateLand(xElement, -yElement, 200, DEFAULT_ELEMENT_HEIGHT_MAX);
        } else if (i < landBucketSize * 3) {
        	lands[i] = generateLand(-xElement, -yElement, 200, DEFAULT_ELEMENT_HEIGHT_MAX);
        } else {
        	lands[i] = generateLand(-xElement, yElement, 200, DEFAULT_ELEMENT_HEIGHT_MAX);
        }
    }
		return lands;
	}

}
